package com.example.Cairodio;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 
 * @author Shokry
 * 
 *         This class is the adapter for the tour List View. The tour has 2
 *         modes, editing and normal. If the tour is in editing mode then the
 *         delete buttons appear and the title of the 2 buttons on top change
 *         from [Map,Edit] to [Done,Add]
 * 
 */
public class TourListAdapter extends BaseAdapter {

	private static ArrayList<String> tourArrayList;
	private LayoutInflater mInflater;
	public boolean editMode;
	Context mContext;

	public TourListAdapter(Context context, ArrayList<String> tourPlacesNames) {
		editMode = false;
		tourArrayList = tourPlacesNames;
		mInflater = LayoutInflater.from(context);
		mContext = context;
	}

	@Override
	public int getCount() {
		return tourArrayList.size();
	}

	@Override
	public Object getItem(int position) {
		return tourArrayList.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	/**
	 * @author Shokry
	 * 
	 *         there are 2 items in each row, an image 'X' that appears only in
	 *         editing mode. and the name of the place, which is fetched from
	 *         the ArrayList "tourArrayList".
	 * 
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;

		convertView = mInflater.inflate(R.layout.edit_tour_item, null);
		holder = new ViewHolder();
		convertView.setMinimumHeight(100);

		holder.txtPlace = (TextView) convertView.findViewById(R.id.tourItem);
		holder.txtPlace.setText(getValue(tourArrayList.get(position)));
		holder.imgDelete = (ImageView) convertView.findViewById(R.id.deleteImg);

		convertView.setTag(holder);

		if (editMode) {
			convertView.findViewById(R.id.deleteImg)
					.setVisibility(View.VISIBLE);
		} else {
			convertView.findViewById(R.id.deleteImg).setVisibility(View.GONE);
		}

		return convertView;
	}

	static class ViewHolder {
		TextView txtPlace;
		ImageView imgDelete;
	}

	/**
	 * @author Shokry
	 * 
	 * This method gets the name of the place from the string.xml file. Thus, it
	 * depends on the locale of the App. there are 2 String arrays used. The
	 * first in english, is used to get the index of place name. This index is
	 * used to get the value of the name in the current language. This method
	 * was used because of the unity of our database (EN)
	 * 
	 * @param place
	 * @return
	 */
	private String getValue(String place) {

		String[] allPlaces = mContext.getResources().getStringArray(
				R.array.places_index);

		int index = 0;
		boolean found = false;

		for (; index < allPlaces.length; index++) {
			if (place.equals(allPlaces[index])) {
				found = true;
				break;
			}
		}

		if (!found)
			return place;

		// Translate
		String[] allPlacesNames = mContext.getResources().getStringArray(
				R.array.places_values);
		return allPlacesNames[index];

	}

}
